package com.binc.testspring.study.mina.study1;

import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * FileName: Myclient   mina编写的客户但
 * Autho: binC
 * Date:  2022/3/25 9:33
 */
@Slf4j
public class CcbClient {

    static  int port = 18089;
    static String host = "221.7.130.87";
    static IoConnector connector = null;

    public static void main(String[] args) throws IOException {
        try {

            connector = new NioSocketConnector();
            connector.setConnectTimeout(3000);
            //设置编码过滤器
            connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(
                    new TextLineCodecFactory(
                            Charset.forName("GB18030"),
                            LineDelimiter.DEFAULT.getValue(),
                            "</TX>")
            ));


            // connector.getSessionConfig().setReadBufferSize(20);
            // connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
            // 注册自己自定义的业务处置对象
            connector.setHandler(new MyClientHandler());

            // 创建链接  链接远程服务器
            ConnectFuture connectFuture = connector.connect(new InetSocketAddress(host, port));

            connectFuture.awaitUninterruptibly();// 无参数的话，一直阻塞到获取链接

            IoSession session = connectFuture.getSession();
String string = "<?xml version=\"1.0\" encoding=\"GB2312\" standalone=\"yes\" ?><TX><REQUEST_SN>942596935052087424</REQUEST_SN><CUST_ID>GZ2000009168314501</CUST_ID><USER_ID>WLPT01</USER_ID><PASSWORD>ado861211</PASSWORD><TX_CODE>6W0100</TX_CODE><LANGUAGE>CN</LANGUAGE><TX_INFO><ACC_NO>52050150383600000006</ACC_NO></TX_INFO></TX>";
            session.write(string);

            //等待关闭链接    // binc_read 这里不是很理解 为什么不自己关闭呢
            session.getCloseFuture().awaitUninterruptibly();

            // 关闭链接
            connector.dispose();

        } catch (Exception e) {
            e.printStackTrace();
        }






    }


}
